<?php

/*
 * Author: Quan Van Sinh
 * Email : qvsinh@gmail.com
 * Mobile: 0972405165
 */

class stream {

    function stream() {
        
    }

    function viewCommentById() {
        global $db;
        $mid = intval($_GET['mid']);
        $id = intval($_GET['id']);
        if ($id) {
            $sql = "SELECT * FROM stream WHERE 1";
            $sql.=" AND mid=$mid AND id=$id";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        }else{
            return array('rs' => FALSE);
        }
    }
    
    function listComment($limit=20) {
        global $db;
        $mid = intval($_GET['mid']);
        $view = trim($_REQUEST['view']);
        if ($view == 'private') {
            $sql = "SELECT * FROM stream WHERE 1 AND (parent_id=0 OR type=3)";
            $sql.=" AND (mid=$mid OR share_for=$mid)";
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        } elseif ($view == 'group') {
            $group_id = intval($_GET['gid']);
            $sqlGeo = "SELECT * FROM member WHERE id=$mid";
            $sql = "SELECT * FROM stream WHERE 1 AND (parent_id=0 OR type=3)";
            if ($group_id) {
                $sql.=" AND (mid=$mid or mid in (share_for))";
            } else {
                $sql.=" AND mid=$mid";
            }
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        } else {
            $sqlGeo = "SELECT * FROM member WHERE id=$mid";
            $arrGeo = $db->query_first($sqlGeo);
            $geo_id = intval($arrGeo['geo_id']);
            $sql = "SELECT * FROM stream WHERE 1 AND (parent_id=0 OR type=3)";
            $sql.=" AND (mid=$mid AND mid in (select id from member where geo_id=$geo_id) AND share_for=0 )";
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        }
    }

    function listCommentFromLastId($limit=20) {
        global $db;
        $mid = intval($_POST['mid']);
        $view = trim($_POST['view']);
        $last_id = intval($_POST['last_id']);
        if ($view == 'private') {
            $sql = "SELECT * FROM stream WHERE 1  AND id<$last_id AND (parent_id=0 OR type=3)";
            $sql.=" AND (mid=$mid OR share_for=$mid)";
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        } elseif ($view == 'group') {
            $group_id = intval($_POST['group_id']);
            $sql = "SELECT * FROM stream WHERE 1 AND id<$last_id AND (parent_id=0 OR type=3)";
            if ($group_id) {
                $sql.=" AND (mid=$mid or mid in (share_for))";
            } else {
                $sql.=" AND mid=$mid";
            }
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        } else {
            $sqlGeo = "SELECT * FROM member WHERE id=$mid";
            $arrGeo = $db->query_first($sqlGeo);
            $geo_id = intval($arrGeo['geo_id']);
            $sql = "SELECT * FROM stream WHERE 1 AND id<$last_id AND (parent_id=0 OR type=3)";
            $sql.=" AND (mid=$mid or mid in (select id from member where geo_id=$geo_id))";
            $sql.=" ORDER BY id DESC LIMIT $limit";
            if ($mid > 0) {
                $rs = $db->query($sql);
            }
            return array('rs' => $rs);
        }
    }

    function listChildComment($parent_id, $limit=20) {
        global $db;

        $sql = "SELECT * FROM stream WHERE 1 AND parent_id=$parent_id ORDER BY id ASC LIMIT $limit";
        if ($parent_id > 0) {
            $rs = $db->query($sql);
            return array('rs' => $rs);
        } else {
            return FALSE;
        }
    }

    function likeStream($mid, $stream_id) {
        global $db;

        $rsCheck = $db->query("SELECT * FROM stream WHERE id=$stream_id AND likes like '%|" . $mid . "|%'");

        if ($db->num_rows($rsCheck) <= 0) {
            $sql = "UPDATE stream SET likes=concat(likes,'" . $mid . "|') WHERE id=$stream_id";
        } else {
            $sql = "UPDATE stream SET likes=replace(likes,'" . $mid . "|','') WHERE id=$stream_id";
        }

        if ($db->query($sql))
            return true;
        else
            return false;
    }

    function listStream($limit=20, $share_for) {

        global $db;
        $sql = "SELECT friend_id from friend where 1 ";
        $arrGroup = explode("|", $groups);
        for ($i = 0; $i < count($arrGroup); $i++) {
            if (count($arrGroup) == 1) {
                $sql.="AND (groups like '%|" . $group_id . "|%')";
            } else {
                if ($i == 0) {
                    $sql.="AND (groups like '%|" . $group_id . "|%'";
                } else if ($i == count($arrGroup) - 1) {
                    $sql.=" or groups like '%|" . $group_id . "|%')";
                } else {
                    $sql.=" or groups like '%|" . $group_id . "|%'";
                }
            }
        }
        $sql.=" order by friend_id";
        $rs = $db->query($sql);
    }
    
    function getStreamById($id) {
        global $db;
        if ($id > 0) {
            $sql = "SELECT * FROM stream where id=$id";
            $rs = $db->query_first($sql);
            return $rs;
        } else {
            return FALSE;
        }
    }
    
    function getSubStreamById($id) {
        global $db;
        if ($image_id > 0) {
            $sqlComment = "SELECT * FROM stream WHERE 1 AND parent_id=$id";
            $rsComment = $db->query($sqlComment);
            while ($row = $db->fetch_array($rsComment)) {
                $rs[$row['mid']] = $row;
            }
            return $rs;
        } else {
            return FALSE;
        }
    }
    
}

?>
